package com.wsj.dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wsj.dao.IMagazineDao;
import com.wsj.util.page.PageResultSet;
import com.wsj.util.page.PerfectPageResultSet;
import com.wsj.util.tool.StringUtils;
import com.wsj.util.tool.Tool;
import com.wsj.web.subclass.displayBean.Magazine.MagazineExtendBean;
import com.wsj.web.vo.homeSite.Magazine;

public class MagazineDaoImpl extends HibernateDaoSupport implements IMagazineDao {

	public void add(Magazine magazine) throws DataAccessException {
		this.getHibernateTemplate().save(magazine);

	}

	public void delete(Integer id) throws DataAccessException {
		Magazine magazine=(Magazine) this.getHibernateTemplate().get(Magazine.class, id);
		this.getHibernateTemplate().delete(magazine);

	}

	public Magazine getMagazineById(Integer id) throws DataAccessException {
		Magazine magazine=(Magazine) this.getHibernateTemplate().get(Magazine.class, id);
		return magazine;
	}

	public Magazine getMagazineByName(String title) throws DataAccessException {
		Magazine magazine=null;
		String sql="from Magazine as m where m.title = ? ";
		List list=this.getHibernateTemplate().find(sql, title);
		if(list!=null&&!list.isEmpty()){
			magazine=(Magazine) list.get(0);
		}
		
		return magazine;
	}
	
	
	@SuppressWarnings("unchecked")
	public List<Magazine> loadByTime(Integer top) throws DataAccessException {
		String sql = "from Magazine order by datetime desc ";
		List list = this.getSession().createQuery(sql).setMaxResults(top).list();
		if(StringUtils.isNotNull(list)){
			return list;
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<Magazine> loadByNameId(Integer nameId, Integer top) throws DataAccessException {
		String sql = "from Magazine as m where m.magazineClass.id = ? and m.shanchu = 0 order by m.iscommend desc, m.datetime desc ";
		List list = this.getSession().createQuery(sql).setInteger(0, nameId).setMaxResults(top).list();
		if(StringUtils.isNotNull(list)){
			return list;
		}
		return null;
	}

	
	public PageResultSet loadMagazineByParam(String title, String content,
			String keyword, Integer hits, String username, Integer iscommend,
			Short isImg, Short shanchu, Short toptitle, Short gs, Short sh,int page,
			int pageSize,Integer magazineNameid,Integer magazineClassid)
			throws DataAccessException {

		
//		 初始化
		PageResultSet pageResultSet = new PageResultSet();

		PerfectPageResultSet pageResult = new PerfectPageResultSet();

		// 获取Session
		Session session = SessionFactoryUtils.getSession(getSessionFactory(),
				true);

		// 初始化SQL语句
		StringBuffer stringBuffer = new StringBuffer("from Magazine as m where 1=1 ");

		// 设置每页显示得记录数和开始页
		pageResult.setPage(page, pageSize);
		
        // 参数
        List<Comparable> list = new ArrayList<Comparable>();
        
        //标题
        if(title!= null&&!title.equals("")){
            stringBuffer.append(" and m.title like ?");
            
            list.add("%"+title+"%");
        }
          //内容
        if(content!= null&&!content.equals("")){
        	stringBuffer.append(" and m.content like ?");
        	
        	list.add("%"+content+"%");
        }
          //关键字
        if(keyword!= null&&!keyword.equals("")){
        	stringBuffer.append(" and m.keyword like ?");
        	
        	list.add("%"+keyword+"%");
        }
        //删除
        if(shanchu!= null&&shanchu!=-2){
        	stringBuffer.append(" and m.shanchu = ?");
        	
        	list.add(shanchu);
        }
        //
        if(magazineNameid!= null&&magazineNameid!=-2){
        	stringBuffer.append(" and m.magazineClass.magazineName.id = ?");
        	
        	list.add(magazineNameid);
        }
        if(magazineClassid!= null&&magazineClassid!=-2){
        	stringBuffer.append(" and m.magazineClass.id = ?");
        	
        	list.add(magazineClassid);
        }
		
		stringBuffer.append(" order by m.id desc ");
		
		 String[] strParams = Tool.toArray(list);
	        pageResultSet = pageResult.pageResultSet(stringBuffer.toString(),
	                session, strParams);

		return pageResultSet;
	}

	public void update(Magazine magazine) throws DataAccessException {
		
		this.getHibernateTemplate().update(magazine);

	}

	public PageResultSet loadMagazineByMagazineExtendBean(MagazineExtendBean magazineExtendBean) throws DataAccessException {
//		 初始化
		PageResultSet pageResultSet = new PageResultSet();

		PerfectPageResultSet pageResult = new PerfectPageResultSet();

		// 获取Session
		Session session = SessionFactoryUtils.getSession(getSessionFactory(),
				true);

		// 初始化SQL语句
		StringBuffer stringBuffer = new StringBuffer("from Magazine as m where 1=1 ");

		// 设置每页显示得记录数和开始页
		pageResult.setPage(magazineExtendBean.getPage(), magazineExtendBean.getPageSize());
		
        // 参数
        List<Comparable> list = new ArrayList<Comparable>();
        
        //标题
        if(magazineExtendBean.getTitle()!= null&&!magazineExtendBean.getTitle().equals("")){
            stringBuffer.append(" and m.title like ?");
            
            list.add("%"+magazineExtendBean.getTitle()+"%");
        }
          //内容
        if(magazineExtendBean.getContent()!= null&&!magazineExtendBean.getContent().equals("")){
        	stringBuffer.append(" and m.content like ?");
        	
        	list.add("%"+magazineExtendBean.getContent()+"%");
        }
          //关键字
        if(magazineExtendBean.getKeyword()!= null&&!magazineExtendBean.getKeyword().equals("")){
        	stringBuffer.append(" and m.keyword like ?");
        	
        	list.add("%"+magazineExtendBean.getKeyword()+"%");
        }
        //删除
        if(magazineExtendBean.getShanchu()!= null&&magazineExtendBean.getShanchu()!=-2){
        	stringBuffer.append(" and m.shanchu = ?");
        	
        	list.add(magazineExtendBean.getShanchu());
        }
        //
        if(magazineExtendBean.getMagazineNameid()!= null&&magazineExtendBean.getMagazineNameid()!=-2){
        	stringBuffer.append(" and m.magazineClass.magazineName.id = ?");
        	
        	list.add(magazineExtendBean.getMagazineNameid());
        }
        if(magazineExtendBean.getMagazineClassid()!= null&&magazineExtendBean.getMagazineClassid()!=-2){
        	stringBuffer.append(" and m.magazineClass.id = ?");
        	
        	list.add(magazineExtendBean.getMagazineClassid());
        }
		
		stringBuffer.append(" order by m.id desc ");
		
		 String[] strParams = Tool.toArray(list);
	        pageResultSet = pageResult.pageResultSet(stringBuffer.toString(),
	                session, strParams);

		return pageResultSet;

	}

	
	/*****
	 * 获取前四个图片杂志.
	 */
	@SuppressWarnings("unchecked")
	public List<Magazine> loadPicMagazineListByMagazineNameIdDao(Integer magazineNameId, Integer top) throws DataAccessException 
	{
		String hql=" from Magazine m where m.magazineClass.magazineName.id = ?  and m.isImg = 1 ";
		Query query =this.getSession().createQuery(hql);
		query.setParameter(0, magazineNameId);
		query.setFirstResult(0);
		query.setMaxResults(top);
		return query.list();
	}

}


